package edu.ccut.saturn.basecomponent.database.impl;

import java.util.ArrayList;
import java.util.List;

import edu.ccut.saturn.basecomponent.database.ISelectSQLBuilder;
import edu.ccut.saturn.basecomponent.database.ISaturnSQL;
import edu.ccut.saturn.basecomponent.database.util.SaturnDataUtil;
import edu.ccut.saturn.component.IDataInfo;
import edu.ccut.saturn.component.IDataItem;

public abstract class AbstractSelectSQLBuilder extends AbstractSQLBuilder
		implements ISelectSQLBuilder {
	public ISaturnSQL build2(String fullKey, String condition) throws Exception {
		return null;
	}

	public ISaturnSQL build2(String fullKey, String condition, String tableWord)
			throws Exception {
		return null;
	}

	public ISaturnSQL build(String fullKey, String condition) throws Exception {

		IDataInfo dataInfo = SaturnDataUtil.getDataInfo(fullKey);

		StringBuffer sql = new StringBuffer();

		String tempSQL = dataInfo.getSqlStr();

		// 如果有默认SQL就取默认SQL
		if (tempSQL != null && !"".equals(tempSQL.trim())) {

			condition = condition.replaceFirst("WHERE", "AND");

			sql.append(tempSQL).append(" ").append(condition);

		} else {
			StringBuffer tableName = new StringBuffer();

			sql.append("SELECT ");
			// 判断是否多表
			if (isMultiQuery(dataInfo)) {

				for (String key : dataInfo.keySet()) {

					IDataItem iDataItem = dataInfo.getDataItem(key);

					String colum = iDataItem.getColumn();

					String columnAlias = iDataItem.getColumnAlias();

					String tableName1 = iDataItem.getTableName();

					if (tableName1 != null && !"".equals(tableName1.trim())) {

						sql.append(tableName1).append(".").append(colum);

						if (columnAlias != null && !columnAlias.equals("")) {

							sql.append(" AS ").append(columnAlias).append(",");

						} else {
							sql.append(",");
						}
					}
				}
				sql.deleteCharAt(sql.length() - 1);

				sql.append(" FROM ");

				List<String> tableNamelist = new ArrayList<String>();

				for (String key : dataInfo.keySet()) {

					IDataItem iDataItem = dataInfo.getDataItem(key);

					String tableName1 = iDataItem.getTableName();

					if (tableName1 != null && !"".equals(tableName1)) {

						if (!tableNamelist.contains(tableName1)) {

							tableNamelist.add(tableName1);
						}
					}
				}
				for (String tableName1 : tableNamelist) {

					sql.append(tableName1).append(",");
				}

				sql.deleteCharAt(sql.length() - 1);
			} else {
				for (String key : dataInfo.keySet()) {

					IDataItem iDataItem = dataInfo.getDataItem(key);

					String colum = iDataItem.getColumn();

					String columeAlias = iDataItem.getColumnAlias();

					if (tableName.length() == 0) {

						if (iDataItem.getTableName() != null
								&& !"".equals(iDataItem.getTableName()))
							tableName.append(iDataItem.getTableName());
					}
					if (!columeAlias.equals("") && columeAlias != null) {
						sql.append(colum).append(" AS ");
						sql.append(columeAlias).append(",");
					} else {
						sql.append(colum).append(",");
					}
				}
				sql.deleteCharAt(sql.length() - 1);
				sql.append(" FROM ").append(tableName);
			}
			if (condition != null)
				sql.append(" ").append(condition);
		}

		SaturnSQL saturnsql = new SaturnSQL();
		saturnsql.setSql(sql.toString());
		saturnsql.setDBtype(this.getDatabaseType());
		saturnsql.setSaturnData(null);
		saturnsql.setSaturnDataFullKey(dataInfo.getDataFullKey());
		saturnsql.setOperationType(this.getOperationType());

		return saturnsql;
	}

	/**
	 * 子类需要重写
	 */
	public ISaturnSQL build(String fullKey, String condition, int startIndex,
			int endIndex) throws Exception {
		return null;
	}

	/**
	 * 子类需要重写
	 */
	public ISaturnSQL build(String fullKey, String condition,
			String partitionWord, int startIndex, int endIndex)
			throws Exception {
		return null;
	}

	private boolean isMultiQuery(IDataInfo dataInfo) {
		String tempTableName = null;
		for (String key : dataInfo.keySet()) {
			IDataItem iDataItem = dataInfo.getDataItem(key);
			if (tempTableName == null) {
				tempTableName = iDataItem.getTableName();
			} else {
				if (!tempTableName.equals(iDataItem.getTableName())) {
					return true;
				}
			}
		}
		return false;
	}
}
